﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerNhomThucPhamDao : INhomThucPhamDao
    {
        public SqlServerNhomThucPhamDao()
        {
            SQLUtility.TableName = "NhomThucPham";
        }

        //Trả về đối tượng báo ăn
        private NhomThucPham MakeNhomThucPham(DataRow row)
        {
            Guid _id = Guid.Parse(row["Id"].ToString());
            string _tenNhomThucPham = row["TenNhomThucPham"].ToString();
            int _schoolID = int.Parse(row["SchoolID"].ToString());
         return new NhomThucPham(_id, _tenNhomThucPham, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<NhomThucPham> MakeNhomThucPhams(DataTable dt)  
        {
            IList<NhomThucPham> list = new List<NhomThucPham>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeNhomThucPham(row));
            }
            return list;
        }
        private DbParameter[] Take(NhomThucPham NhomThucPham)
        {
            DbParameter[] parameters = new DbParameter[3];
            parameters[0] = new SqlParameter("@Id", NhomThucPham.Id);
            parameters[1] = new SqlParameter("@TenNhomThucPham", NhomThucPham.TenNhomThucPham);
            parameters[2] = new SqlParameter("@SchoolID", NhomThucPham.SchoolID);
            return parameters;
        }

        public IList<Business_Objects.NhomThucPham> GetNhomThucPham()
        {
            SQLUtility.TableName = "NhomThucPham";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeNhomThucPhams(dt); 
        }

        public Business_Objects.NhomThucPham GetNhomThucPhamByID(string NhomThucPhamID)
        {
            SQLUtility.TableName = "NhomThucPham";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", NhomThucPhamID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeNhomThucPham(row);
            }
        }

        public void InsertNhomThucPham(Business_Objects.NhomThucPham NhomThucPham)
        {
            SQLUtility.TableName = "NhomThucPham";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(NhomThucPham), true);
        }

        public int UpdateNhomThucPham(Business_Objects.NhomThucPham NhomThucPham)
        {
            SQLUtility.TableName = "NhomThucPham";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(NhomThucPham)); }
            catch { return -1; }
        }

        public int DeleteNhomThucPham(Business_Objects.NhomThucPham NhomThucPham)
        {
            SQLUtility.TableName = "NhomThucPham";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(NhomThucPham)); }
            catch { return -1; }
        }
    }
}
